// 封装接口方法
import axios from '@/utils/request.js'
// import { getToken } from '@/utils/token.js'

// 频道--获取所有频道列表
export const getAllChannelsAPI = () => {
  return axios({
    url: '/v1_0/channels'
  })
}

// axios内部，会把参数对象转成json字符串发送给后台
// Content-Type:'application/json'会自动帮你添加好
// 登录功能
export const loginAPI = ({ mobile, code }) => {
  return axios({
    url: '/v1_0/authorizations',
    method: 'POST',
    data: {
      mobile,
      code
    }
  })
}

// 频道-获取用户选择的频道
// 用户没登录会默认返回频道列表
export const getUserChannelsAPI = () => {
  return axios({
    url: '/v1_0/user/channels'
    // headers: {
    //   Authorization: `Bearer ${getToken()}`
    // }
  })
}

// 用户关注

export const userFollowedAPI = (userId) => {
  return axios({
    url: '/v1_0/user/followings',
    method: 'POST',
    data: {
      target: userId
    }
  })
}

// 取消关注
export const unFollowedAPI = (userId) => {
  return axios({
    url: `/v1_0/user/followings/${userId}`,
    method: 'DELETE'
  })
}

// 频道-更新覆盖频道
export const updateChannelAPI = ({ channels }) => {
  return axios({
    url: '/v1_0/user/channels',
    method: 'PUT',
    data: {
      channels // 用户已选的频道数组
    }
  })
}

// 频道-删除用户指定频道
export const removeChannelAPI = (channel_id) => {
  return axios({
    url: `/v1_0/user/channels/${channel_id}`,
    method: 'DELETE'
  })
}

// 文章-获取文章新闻推荐
export const getAllArticleListAPI = ({ channel_id, timestamp }) => {
  return axios({
    url: '/v1_0/articles',
    // headers: {
    //   Authorization: `Bearer ${getToken()}`
    // },
    params: {
      channel_id,
      timestamp
    }
  })
}

// -文章-对文章不喜欢
export const dislikeArticleAPI = (act_id) => {
  return axios({
    url: '/v1_0/article/dislikes',
    method: 'POST',
    // headers: {
    //   Authorization: `Bearer ${getToken()}`
    // },
    data: {
      target: act_id
    }
  })
}

// -文章-举报文章
export const reportArticleAPI = ({ art_id, type }) => {
  return axios({
    url: '/v1_0/article/reports',
    method: 'POST',
    // headers: {
    //   Authorization: `Bearer ${getToken()}`
    // },
    data: {
      target: art_id,
      type: type,
      remark: '这是默认'
    }
  })
}

// 文章-文章点赞
export const articleGood = (art_id) => {
  return axios({
    url: '/v1_0/article/likings',
    method: 'POST',
    data: {
      target: art_id
    }
  })
}

// 文章-文章取消点赞
export const articleUnGood = (art_id) => {
  return axios({
    url: `/v1_0/article/likings/${art_id}`,
    method: 'DELETE'
  })
}

// 文章-获取文章评论列表
export const commentsListAPI = ({ art_id, offset = null, limit = 10 }) => {
  return axios({
    url: '/v1_0/comments',
    params: {
      type: 'a',
      source: art_id,
      offset: offset,
      limit: limit
    }
  })
}

// 文章- 对文章进行评论
export const sendCommentsAPI = ({ art_id, content }) => {
  return axios({
    url: '/v1_0/comments',
    method: 'POST',
    data: {
      target: art_id,
      content: content,
      art_id: art_id
    }
  })
}

// 文章- 收藏文章
export const collectionArticleAPI = (art_id) => {
  return axios({
    url: '/v1_0/article/collections',
    method: 'POST',
    data: {
      target: art_id
    }
  })
}

// 文章- 取消收藏文章id
export const cancelCollectionArticleAPI = (art_id) => {
  return axios({
    url: `/v1_0/article/collections/${art_id}`,
    method: 'DELETE'
  })
}

// 文章- 对评论或评论回复点赞
export const responseCommentsList = (id) => {
  return axios({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: id // 点赞的评论id
    }

  })
}

// 文章-取消对评论或评论回复点赞
export const unresponseCommentsList = (id) => {
  return axios({
    url: `/v1_0/comment/likings/${id}`,
    method: 'DELETE'
  })
}

// 新闻-获取文章详情
export const ArticleDetailAPI = (art_id) => {
  return axios({
    url: `/v1_0/articles/${art_id}`
  })
}

// 搜索 联想搜索菜单
export const suggestListAPI = ({ keywords }) => {
  return axios({
    url: '/v1_0/suggestion',
    params: {
      q: keywords
    }
  })
}

// 搜索--搜索结果列表

export const searchResultAPI = ({ page = 1, per_page = 10, q }) => {
  return axios({
    url: '/v1_0/search',
    params: {
      page: page,
      per_page: per_page,
      q: q
    }
  })
}

// 用户- 获取用户个人信息
export const getUserInfoAPI = () => {
  return axios({
    url: '/v1_0/user'
  })
}

// 用户- 获取用户资料
export const getUserProfileAPI = () => {
  return axios({
    url: '/v1_0/user/profile'
  })
}

// 用户- 照片修改
export const changeUserPhotoAPI = (photo) => {
  return axios({
    url: '/v1_0/user/photo',
    method: 'PATCH',
    // 请求体式formdata表单对象
    data: photo // 表单对象
  })
}

// 用户 - 更新资料
export const updateProfileAPI = (dataObj) => {
  const obj = {
    name: '',
    gender: 0,
    birthday: '',
    intro: ''
  }
  for (const prop in obj) {
    if (dataObj[prop] === undefined) {
      delete obj[prop]
    } else {
      obj[prop] = dataObj[prop]
    }
  }
  return axios({
    url: '/v1_0/user/profile',
    method: 'PATCH', // patch局部更新，put全部更新
    data: obj
    // data: {
    //   birthday: birthday,
    //   name: userName
    // }
  })
}
